#! /usr/bin/python

# Bird_upYear.py

import wx
import os
import ftp_birdlist

ID_DEPTH = 1

class BirdUpYear(wx.Dialog):
    """Post birdlist to a website"""
    def __init__(self, parent, id, title='Upload birdlist to a website'):
        wx.Dialog.__init__(self, parent, id, title, size=(500, 420))
        
        # Set the fonts
        self.dtitleFont = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
        self.dtitleFont.SetPointSize(18)
        self.dlicenceFont = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
        self.dlicenceFont.SetPointSize(9)
        
        # Start working on the gui framework
        self.dpanel = wx.Panel(self, -1)
        dvbox = wx.BoxSizer(wx.VERTICAL)
        
        # First level - Title
        dhbox1 = wx.BoxSizer(wx.HORIZONTAL)        
        self.dtitle = "Blu Bird List Uploader 0.1a"
        self.dtitle_txt = wx.StaticText(self.dpanel, -1, self.dtitle)
        self.dtitle_txt.SetFont(self.dtitleFont)
        dhbox1.Add(self.dtitle_txt, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 8)
        dvbox.Add(dhbox1, 0, wx.ALIGN_CENTER)
        
        # Breathing room
        dvbox.Add((-1, 25))        
        
        # Choose the file to be uploaded
        # fullpath usr="........", psw=".........."):
        
        dhbox2 = wx.BoxSizer(wx.HORIZONTAL)        
        self.descript = wx.StaticText(self.dpanel, -1, "File to Upload  ",  style=wx.EXPAND)
        self.descript.SetFont(self.dlicenceFont)
        self.fileButton = wx.Button(self.dpanel, -1, "Browse", size=(80, 30))
        _path = os.getcwd()
        self.web_path = "%s%s%s" % (_path, os.sep, 'life_list.htm')
        self.theFile = wx.TextCtrl(self.dpanel, -1, value=self.web_path, size=(300, 30))
        dhbox2.Add(self.descript, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 8)
        dhbox2.Add(self.theFile, 1) 
        dhbox2.Add(self.fileButton, 0, wx.EXPAND, 8)
        dvbox.Add(dhbox2, 0, wx.ALIGN_CENTER)
        self.Bind(wx.EVT_BUTTON, self.OnBrowse, id=self.fileButton.GetId())
        
        # Breathing room
        dvbox.Add((-1, 25))
        
        # Choose the host
        # host="sandcurves.com", 
        dhbox3 = wx.BoxSizer(wx.HORIZONTAL)        
        self.descript = wx.StaticText(self.dpanel, -1, "Host name  ",  style=wx.EXPAND)
        self.descript.SetFont(self.dlicenceFont)
        self.theHost = wx.TextCtrl(self.dpanel, -1, size=(300, 30))
        dhbox3.Add(self.descript, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 8) 
        dhbox3.Add(self.theHost, 1)  
        dvbox.Add(dhbox3, 0, wx.ALIGN_CENTER)
        
        # Breathing room
        dvbox.Add((-1, 25))
        
        # Choose the host
        # hostDir="/public_html/birdlist"
        dhbox4 = wx.BoxSizer(wx.HORIZONTAL)        
        self.descript = wx.StaticText(self.dpanel, -1, "Host directory  ",  style=wx.EXPAND)
        self.descript.SetFont(self.dlicenceFont)
        self.hostDir = wx.TextCtrl(self.dpanel, -1, size=(300, 30))
        dhbox4.Add(self.descript, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 8) 
        dhbox4.Add(self.hostDir, 1)  
        dvbox.Add(dhbox4, 0, wx.ALIGN_CENTER)
        
        # Breathing room
        dvbox.Add((-1, 25))
        
        # Choose the host
        # usrname = 'sandcurv'
        dhbox5 = wx.BoxSizer(wx.HORIZONTAL)        
        self.descript = wx.StaticText(self.dpanel, -1, "User Name  ",  style=wx.EXPAND)
        self.descript.SetFont(self.dlicenceFont)
        self.username = wx.TextCtrl(self.dpanel, -1, size=(300, 30))
        dhbox5.Add(self.descript, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 8) 
        dhbox5.Add(self.username, 1)  
        dvbox.Add(dhbox5, 0, wx.ALIGN_CENTER)
        
        # Breathing room
        dvbox.Add((-1, 25))
        
        # Choose the host
        # password = '..........'
        dhbox6 = wx.BoxSizer(wx.HORIZONTAL)        
        self.descript = wx.StaticText(self.dpanel, -1, "Password  ",  style=wx.EXPAND)
        self.descript.SetFont(self.dlicenceFont)
        self.password = wx.TextCtrl(self.dpanel, -1, size=(300, 30))
        dhbox6.Add(self.descript, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 8) 
        dhbox6.Add(self.password, 1)  
        dvbox.Add(dhbox6, 0, wx.ALIGN_CENTER)
        
        # Breathing room
        dvbox.Add((-1, 25))
        
        #Buttons
        dhbox_buttons = wx.BoxSizer(wx.HORIZONTAL)       
        dexit_button = wx.Button(self.dpanel, -1, "Upload Birdlist", size=(120, 30))
        dhbox_buttons.Add(dexit_button, 0)
        dvbox.Add(dhbox_buttons, 0, wx.ALIGN_RIGHT | wx.RIGHT, 10)
        self.Bind(wx.EVT_BUTTON, self.OnOkay, id=dexit_button.GetId())
        
        # Place my panel
        self.dpanel.SetSizer(dvbox)        
        
        # End of the structure part of the module
        
    def OnBrowse(self, event):
        application = wx.PySimpleApp()
        # Create an open file dialog
        dialog = wx.FileDialog(None, style=wx.OPEN)
        # Show the dialog and get user input
        if dialog.ShowModal() == wx.ID_OK:
            upfile = dialog.GetPath()
        # If the user did not select anything
        # The file remains on the default value
        # Destroy the dialog
        dialog.Destroy()
        if upfile:
            self.theFile.SetValue(upfile)
      
    def OnOkay(self, event):
        """Upload the file to the website and close this dialog"""
        fullpath = self.theFile.GetValue()
        host = self.theHost.GetValue()
        hostDir = self.hostDir.GetValue()
        usr = self.username.GetValue()
        psw = self.password.GetValue()
        upload = ftp_birdlist.UploadBirdlist(fullpath, host, hostDir, usr, psw)
        x = upload.ftp_upload()
        if x == 1:
            wx.MessageBox('Upload completed', 'Info')
        else:
            dial = wx.MessageDialog(None, 'Error Uploading', 'Error', wx.OK | 
            wx.ICON_ERROR)
            dial.ShowModal()
        self.Close()
        
# End module